package be.bonamis.umpire.ui.client;

import be.bonamis.umpire.ui.client.gin.UmpireGinjector;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

public class Application implements EntryPoint {
	private static UmpireGinjector ginjector;
	private SimplePanel appWidget = new SimplePanel();
	
	/**
	 * This is the entry point method.
	 */	
	public void onModuleLoad() {
		ginjector = GWT.create(UmpireGinjector.class);
		final ClientFactory clientFactory = ginjector.getClientFactory();		
		
		AppActivityMapper mapper = new AppActivityMapper(clientFactory);
		
		ActivityManager manager = new ActivityManager(mapper, clientFactory.getEventBus());
		manager.setDisplay(appWidget);
		
		//LoginView view = clientFactory.getLoginView();
		
		//RootPanel.get().add(view.asWidget());
		RootPanel.get().add(appWidget);
		//manager.setDisplay();
		
		clientFactory.getPlaceHistoryHandler().handleCurrentHistory();

//		start(clientFactory, new Command() {
//			@Override
//			public void execute() {
//				RootPanel.get().add(clientFactory.getLoginView().asWidget());
//			}
//		});

	}

	public static UmpireGinjector getInjector() {
		return ginjector;
	}

	public void start(final ClientFactory clientFactory) {
		start(clientFactory, null);
	}

	public void start(final ClientFactory clientFactory, final Command postInitCommand) {

	}

	// public void start(final ClientFactory clientFactory) {
	// start(clientFactory, null);
	// }

	// RootPanel.get().add(ginjector.getLoginPresenter().getDisplay().asWidget());
	// ActionServiceWrapper wrapper = ginjector.getActionServiceWrapper();

	// clientFactory = new ClientFactoryImpl();
	//
	// // Start ActivityManager for the main widget with our ActivityMapper
	// AppActivityMapper activityMapper = new AppActivityMapper(clientFactory);
	// ActivityManager activityManager = new ActivityManager(activityMapper,
	// clientFactory.getEventBus());
	// activityManager.setDisplay(appWidget);
	//
	// // Start PlaceHistoryHandler with our PlaceHistoryMapper
	// AppPlaceHistoryMapper historyMapper = GWT
	// .create(AppPlaceHistoryMapper.class);
	// PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
	// historyMapper);
	// historyHandler.register(clientFactory.getPlaceController(),
	// clientFactory.getEventBus(), defaultPlace);

	// RootPanel.get().add(appWidget);

	// Goes to the place represented on URL else default place
	// historyHandler.handleCurrentHistory();

	// public ClientFactory getClientFactory() {
	// return clientFactory;
	// }
}
